﻿Imports Microsoft.VisualBasic
Imports System.Web.Security
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Imports System.Globalization


Namespace Creasys.Utility
    Public Class DesEncrypt

        '加密方法
        Public Function EncryptString(ByVal pToEncrypt As String, ByVal sKey As String) As String
           

            Dim data As Byte() = Encoding.UTF8.GetBytes(pToEncrypt)
            Dim DES As New DESCryptoServiceProvider()
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
            Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
            Dim result As Byte() = desencrypt.TransformFinalBlock(data, 0, data.Length)
            Return (BitConverter.ToString(result).Replace("-", ""))
            ' Return (BitConverter.ToString(result))

        End Function

        '解密方法
        Public Function DecryptString(ByVal pToDecrypt As String, ByVal sKey As String) As String
            Try
                Dim sInput((pToDecrypt.Length / 2) - 1) As String

                For ii As Integer = 0 To (pToDecrypt.Length / 2) - 1
                    sInput(ii) = pToDecrypt.Substring(2 * ii, 2)
                Next

                Dim data As Byte() = New Byte(sInput.Length - 1) {}
                For i As Integer = 0 To sInput.Length - 1
                    data(i) = Byte.Parse(sInput(i), NumberStyles.HexNumber)
                Next
                Dim DES As New DESCryptoServiceProvider()
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
                Dim desencrypt As ICryptoTransform = DES.CreateDecryptor()
                Dim result As Byte() = desencrypt.TransformFinalBlock(data, 0, data.Length)
                Return Encoding.UTF8.GetString(result)
            Catch ex As Exception
                Return ex.Message
            End Try


       
        End Function

    End Class
End Namespace


